import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

import 'new_material_desktop_controls.dart';

///视频播放
class VideoPlayWidget extends StatefulWidget {
  final String videoUrl;

  const VideoPlayWidget({super.key, required this.videoUrl});

  @override
  State<VideoPlayWidget> createState() => _VideoPlayWidgetState();
}

class _VideoPlayWidgetState extends State<VideoPlayWidget> {
  late VideoPlayerController _videoPlayerController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(widget.videoUrl))
      ..initialize().then((value) {
        setState(() {});
      });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      alignment: Alignment.center,
      child: AspectRatio(
        aspectRatio: 16 / 9,
        child: _videoPlayerController.value.isInitialized
            ? Stack(
                alignment: Alignment.center,
                children: [
                  Chewie(
                      controller: ChewieController(
                          videoPlayerController: _videoPlayerController,
                          aspectRatio: _videoPlayerController.value.aspectRatio,
                          autoPlay: true,
                          looping: false,
                          showControlsOnInitialize: false,
                          allowFullScreen: false,
                          allowMuting: false,
                          customControls: const NewMaterialDesktopControls(),
                          materialProgressColors: ChewieProgressColors(
                              bufferedColor: Colors.transparent,
                              playedColor: Colors.transparent,
                              handleColor: Colors.transparent,
                              backgroundColor: Colors.transparent))),
                ],
              )
            : const CircularProgressIndicator(),
      ),
    );
  }
}

//自定义播放器界面
class MyMaterialDesktopControls extends MaterialDesktopControls {
  const MyMaterialDesktopControls({super.key});
}
